from pylons import request, response, session, tmpl_context as c
from blog.model.meta import Session
from blog.model.user import User,Right
user_q=Session.query(User)

def has_right(way,value):
    def run(*args,**kwargs):
        if 'user' in session :
            user=user_q.filter_by(id=session['user'].id).first()
        else :
            return False
        right=user.Right[0]
        if way == 'manage' :
            if right.manageright & (1 << value) :
                return True
            else :
                return False
        else:
            if getattr(right,'%sright' % way) >= value :
                return True
            else :
                return False
    return run

def is_self(*args,**kwargs):
    uid=kwargs.get('uid',None)
    if not uid :
        return False
    if 'user' in session :
        user=session['user']
    else :
        return False
    return user.id==id

def default_usage(*args,**kwargs):
    uid=kwargs.get('uid',None)
    return uid is None
